clear
use "./macro_all.dta" // 

gen lndfx = ln(dfx)
gen lnafx = ln(afx)
tsset year

****** BEEF ********************************************************************
gen lnh_b = ln(pxdi_b/pxfi_b)
gen lnrq_b = ln(pdi_b/pfi_b)
gen lnpdi_b = ln(pdi_b)
gen lnpfi_b = ln(pfi_b)

dfuller lnh_b
dfuller lnrq_b
egranger lnh_b lnrq_b //cannot reject H0: no cointegration -> take differences

* Main regression 
ivreg2 d.lnh_b (d.lnrq_b = d.pfi_b d.lnpfi_b), robust endog(d.lnrq_b)
reg d.lnh_b d.lnrq_b, robust
reg d.lnpdi_b d.lnpfi_b  // interaction between pfi and pdi  NO
reg d.lnpdi_b d.lnpfi_b, robust  // interaction between pfi and pdi YES
* Delta Method
nlcom 1-_b[d.lnrq_b]

****** CHICKEN *****************************************************************
gen lnh_c = ln(pxdi_c/pxfi_c)
gen lnrq_c = ln(pdi_c/pfi_c)
gen lnpdi_c = ln(pdi_c)
gen lnpfi_c = ln(pfi_c)

dfuller lnh_c //
dfuller lnrq_c //

egranger lnh_c lnrq_c //rejectable @10% H0: no cointegration -> cointegration!

* Main regression
ivreg2 lnh_c (lnrq_c = lnpfi_c pfi_c), robust endog(lnrq_c)
reg lnh_c lnrq_c, robust
reg lnpdi_c lnpfi_c  // interaction between pfi and pdi NO
reg lnpdi_c lnpfi_c, robust  // interaction between pfi and pdi NO
* Delta Method
nlcom 1-_b[lnrq_c]
nlcom exp(_b[_cons])/(1+exp(_b[_cons]))

****** PORK-R ******************************************************************
gen lnh_pR = ln(pxdi_p/pxfi_pr) // Share Ratio
gen lnrq_pR = ln(pdi_p/pfi_pr)  // Price Ratio
gen lnpfi_pR = ln(pfi_pr)     // Foreign price index (Pork(1))
gen lnpdi_p = ln(pdi_p)       // Domestic price index

egranger lnh_pR lnrq_pR //cannot reject H0: no cointegration -> take difference

ivreg2 d.lnh_pR (d.lnrq_pR = d.lnpfi_pR d.pfi_pr), robust endog(d.lnrq_pR)  // iv is weak
reg d.lnpdi_p d.lnpfi_pR  // interaction between pfi and pdi      NO
reg d.lnpdi_p d.lnpfi_pR, robust  // interaction between pfi and pdi  YES
* Suppose the expvar is exogenous in regard to the unrobust reg above *
* Main regression
reg d.lnh_pR d.lnrq_pR, robust
* Delta Method
nlcom 1-_b[d.lnrq_pR]
